<template>
  <div>
    <el-card>
      <!-- 面包屑 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索表单 -->
      <el-form :inline="true" :model="UserQuery" class="demo-form-inline" size="large">
        <el-form-item label="昵称">
          <el-input v-model="UserQuery.nickname" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary">搜索</el-button>
          <el-button type="success">新增用户</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 12px">
      <el-table :data="list" style="width: 100%">
        <el-table-column label="用户名" prop="adminname" />
        <el-table-column label="用户昵称" prop="nickname" />
        <el-table-column label="用户头像" prop="avatar">
          <!-- scope固定写法，是一个对象，里面有个属性row,代表着这一行的数据 -->
          <!-- 作用域插槽，能获取值 -->
          <template #default="scope">
            <img :src="scope.row.avatar" style="width: 50px; height: 50px" />
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch
              disabled
              :model-value="row.status"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary">修改</el-button>
            <el-button type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-config-provider :locale="locale">
        <el-pagination
          v-model:current-page="UserQuery.page"
          v-model:page-size="UserQuery.pageSize"
          :page-sizes="[3, 4, 5, 6]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      /></el-config-provider>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { UserQuery, User } from '@/types/user'
import { getUserList } from '@/api/user'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'

const UserQuery = ref<UserQuery>({ page: 1, pageSize: 3, nickname: null })
const list = ref<User[]>([])
const total = ref(0)
const language = ref('zh-cn')

const getUserListData = async () => {
  const res = await getUserList(UserQuery.value)
  list.value = res.data.data.items
  total.value = res.data.data.total
}
getUserListData()
//每页多少条改变了
const handleSizeChange = (pageSize: number) => {
  UserQuery.value.pageSize = pageSize
  getUserListData()
}
//当前页发生改变
const handleCurrentChange = (page: number) => {
  UserQuery.value.page = page
  getUserListData()
}
//改变文字语言
const locale = computed(() => (language.value === 'zh-cn' ? zhCn : en))
</script>

<style scoped lang="less">
.demo-form-inline {
  margin-top: 20px;
}

:deep(.el-pagination) {
  height: 60px;
  line-height: 60px;
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
</style>
